<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 数组扩展方法是在es5出现的

        console.log('--------------------forEach----------------------');
        // forEach 数组遍历方法
        // Array.prototype.forEach
        // 第二个参数，可以改变this指向
        ['a', 'b', 'c', 'd', 'e'].forEach(function(elem, index, array) {
            console.log(elem);
            console.log(index);
            console.log(array);
            console.log(this);
        }, {
            name: 'test'
        })
        Array.prototype.myForEach = function(fn) {
            var arr = this,
                len = arr.length,
                arg2 = arguments[1] || window;
            for (var i = 0; i < len; i++) {
                fn.apply(arg2, [arr[i], i, arr]);
            }
        }
        console.log('--------------------myForEach----------------------');
        ['a', 'b', 'c', 'd', 'e', '12'].myForEach(function(elem, index, array) {
            console.log(elem);
            console.log(index);
            console.log(array);
            console.log(this);
        }, {
            name: 'test'
        })
        console.log('--------------------filter----------------------');
        // filter 筛选，过滤，返回一个新的数组
        var arr = ['a', 'b', 'c', 'd', 'e', '12'].filter(function(elem, index, array) {
            console.log(elem);
            console.log(index);
            console.log(array);
            console.log(this);
            return elem > 2
        }, {
            name: 'test'
        })
        console.log(arr);
        Array.prototype.myFilter = function(fn) {
            var arr = this,
                len = arr.length,
                arg2 = arguments[1] || window,
                nArr = [],
                item;
            for (var i = 0; i < len; i++) {
                // 深拷贝
                item = deepClone(arr[i])
                fn.apply(arg2, [arr[i], i, arr]) ? nArr.push(item) : '';
            }
            return nArr
        }
        var arr = ['a', 'b', 'c', 'd', '34', '12'].myFilter(function(elem, index, array) {
            console.log(elem);
            console.log(index);
            console.log(array);
            console.log(this);
            return elem > 2
        }, {
            name: 'test'
        })
        console.log(arr);
        console.log('--------------------map----------------------');
        // map 映射
        var arr = ['a', 'b', 'c', 'd', '34', '12'].map(function(elem, index, array) {
            console.log(elem);
            console.log(index);
            console.log(array);
            console.log(this);
            return elem + 2
        }, {
            name: 'test'
        })
        console.log(arr);
        Array.prototype.myMap = function(fn) {
            var arr = this,
                len = arr.length,
                arg2 = arguments[1] || window,
                nArr = [],
                item;
            for (let i = 0; i < len; i++) {
                item = deepClone(arr[i])
                nArr.push(fn.apply(arg2, [item, i, arr]))
            }
        }

        function deepClone() {}
    </script>
</body>

</html>